/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package POS_Controller;

import Cart.Cart;
import Customer.CustomerManager;
import DTO.Item;
import DTO.Customer;
import DAO.DAO;
import DTO.Category;
import DTO.Payment;
import Exception.KwickKioskDBException;
import POS_Source.*;
import POS_Source.mainFrame;
import PaymentTest.PaymentStub;
import Session.POS_SessionManager;
import Session.SessionManager;
import java.awt.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JApplet;
import javax.swing.SwingUtilities;
/**
 *
 * @author satkins
 */
public class POSController extends JApplet{

    private POS_MainOut frame;
    private Container pane;
    private POSController posControl;
    private POS_SessionManager sessionManager;
    private DAO dao;

    public POSController(){
        
    }

    public void init() {
        try {
            dao = new DAO();
        } catch (KwickKioskDBException ex) {
            //NEED TO IMPLEMENT ERROR HANDLING
            Logger.getLogger(POSController.class.getName()).log(Level.SEVERE, null, ex);
        }
        sessionManager = new POS_SessionManager(dao);
        frame = new POS_MainOut(this);
        //Execute a job on the event-dispatching thread; creating this applet's GUI.
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {

                   // moneyFormatter = new MoneyFormatter();
                  //  mainFrame.setBounds(0, 0, 1065, 800);
                    add(frame);
                    SearchChoiceScreen();
                 
                }
            });
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }


    /* GETTERS AND SETTERS ****************************************************/

    public void setLoggedIn(boolean val){
        sessionManager.setLoggedIn(val);
    }

    public ArrayList<Item> getSearchResults(){
        return sessionManager.getCart().getSearchResults();
    }

    public boolean isLoggedIn() {
        return sessionManager.getLoggedIn();
    }

    public String getFirstName(){
        return sessionManager.getCustomer().getFirstName();
    }

    public String getNumItemsInCart(){
        return Integer.toString(sessionManager.getCart().getTotalNumItems());
    }

    public Cart getCart(){
        return sessionManager.getCart();
    }

    public ArrayList<Category> getCategories(){
        ArrayList<Category> categories = null;
        try {
            categories = dao.getCategories();
        } catch (KwickKioskDBException ex) {
            System.out.println("NEED TO IMPLEMENT error handling here");
            Logger.getLogger(POSController.class.getName()).log(Level.SEVERE, null, ex);
        }
        return categories;
    }

    public POS_SessionManager getSessionManager() {
        return sessionManager;
    }


    /* FUNCTIONALITY **********************************************************/

    public void searchByUPC(String word){
        sessionManager.searchByUPC(word);
        SearchResultsScreen();
    }

    public void searchByCategory(int ctid){
        sessionManager.searchByCategory(ctid);
        SearchResultsScreen();
    }
    
    public void addItemToCart(Item item, int quantity){
        sessionManager.addItemToCart(item, quantity);
    }

    public boolean addStoreCard(String storeCard){
        if(sessionManager.setCustomer(storeCard)){
            changeFrame();
            CartScreen();
            return true;
        }
        else
            return false;
    }

    public void removeStoreCard(){
        sessionManager.logout();
        changeFrame();
    }

    public boolean clerkLogin(String id, String password){
        return sessionManager.clerkLogIn(id, password);
    }

    public void clerkLogout(){
        sessionManager.clerkLogout();
        changeFrame();
    }

    public void applyDiscount(){
        
    }

    public void makeCCPayment(double amount){

        PaymentStub stub = new PaymentStub();
        Payment pymnt = stub.getCreditCardPayment(amount);
        sessionManager.addPayment(pymnt);
        if (sessionManager.enoughPayment())
            finishTransaction();
        else
            PaymentSelectScreen();
    }

    private void finishTransaction(){
        ConfirmPaymentScreen();
        sessionManager.fullfillOrder();
        refresh();
    }



    /** SCREENS ***************************************************************/

    public void refresh(){
        frame.setTotals();
        frame.repaint();
    }

    public void POSSearchByUPCScreen() {
        POS_SearchByUPC search = new POS_SearchByUPC(this);
        frame.changeScreen(search);

    }

    public void AddStoreCardScreen(){
        POS_CustomerLogin addCard = new POS_CustomerLogin(this);
        frame.changeScreen(addCard);

    }

    public void SearchResultsScreen(){
        POS_SearchResults resultsScreen = new POS_SearchResults(this);
        frame.changeScreen(resultsScreen);

    }

    public void ItemDescriptionScreen(Item selection){
        
        POS_ItemDescription itemDescription = new POS_ItemDescription(this, selection);
        frame.changeScreen(itemDescription);

    }

    public void SearchChoiceScreen(){
        POS_SearchChoice choice = new POS_SearchChoice(this);
        frame.changeScreen(choice);
    }

    public void SearchCategoryScreen(){
        POS_SearchByCategory categoryScreen = new POS_SearchByCategory(this);
        frame.changeScreen(categoryScreen);

    }

    public void CartScreen(){
        POS_ViewCart cartScreen = new POS_ViewCart(this);
        frame.changeScreen(cartScreen);

    }

    public void ClerkLoginScreen(){
        POS_ClerkLogin login = new POS_ClerkLogin(this);
        frame.changeScreen(login);

    }

    public void ClerkDiscountScreen(){
        POS_ClerkDiscount login = new POS_ClerkDiscount(this);
        frame.changeScreen(login);

    }

    public void PaymentSelectScreen(){
        POS_PaymentSelect select = new POS_PaymentSelect(this);
        frame.changeScreen(select);

    }

    public void CardAmountScreen(){
        POS_CheckoutCardAmount cardAmount = new POS_CheckoutCardAmount(this);
        frame.changeScreen(cardAmount);

    }

    public void PaymentInProcessScreen(){
        POS_CheckoutCard inProcess = new POS_CheckoutCard();
        frame.changeScreen(inProcess);


    }

    public void ConfirmPaymentScreen(){
        POS_ConfirmPayment success = new POS_ConfirmPayment();
        frame.changeScreen(success);
 
    }

    /**
     *
     * @param choice
     * when choice is 1, mainOut become the large panel
     * when choice is 2, main in becomes the large panel
     * when choice is 3, clerkLoggedIn becomes the large panel
     */
    public void changeFrame(){
        frame.changeScreen(new POS_ViewCart(this));
//frame.mainOutScreen();
      /*  if(!sessionManager.getLoggedIn())
        {

            frame.mainOutScreen();
        }
        else{
            frame.mainInScreen();
        }
*/
    }

}
